﻿namespace Nomo.UnityEditorModule
{
    internal static class EditorUtility
    {
        [UnityEditor.MenuItem("Assets/打开文件夹（Windows）")]
        private static void OpenInExplorer()
        {
            try
            {
                var fullPath = $"{UnityEngine.Application.dataPath.Substring(0, UnityEngine.Application.dataPath.IndexOf("Assets", System.StringComparison.Ordinal)).TrimEnd('/', '\\')}/{UnityEditor.AssetDatabase.GetAssetPath(UnityEditor.Selection.activeInstanceID)}";
                if (System.IO.File.GetAttributes(fullPath).HasFlag(System.IO.FileAttributes.Directory))
                {
                    System.Diagnostics.Process.Start("explorer.exe", fullPath.Replace('/', '\\'));
                }
                else
                {
                    UnityEditor.EditorUtility.RevealInFinder(fullPath);
                }
            }
            catch
            {
                UnityEngine.Debug.LogError("此操作引发了异常。");
            }
        }
    }
}
